import re
import pandas as pd
import csv

def list_to_csv(data, filename):
    with open(filename, 'w', newline='', encoding='utf-8') as csvfile:
        writer = csv.writer(csvfile)
        for row in data:
            writer.writerow(row)

def extract_numbers_from_attributes(attributes):
    numbers_list = []
    
    for attribute in attributes:
        attribute = attribute.strip('frozenset()')  
        attribute_list = attribute.split(',')  
        numbers = []
        
        for element in attribute_list:
            element = element.strip()
            if element.isdigit():  
                numbers.append(int(element))
        
        numbers_list.append(numbers)
    
    return numbers_list

def remove_empty_rows(filename):
    with open(filename, 'r', newline='', encoding='utf-8') as file:
        lines = file.readlines()

    with open(filename, 'w', newline='', encoding='utf-8') as file:
        writer = csv.writer(file)
        for line in lines:
            if line.strip():  # 检查行是否为空行
                writer.writerow(line.strip().split(','))


result = pd.read_csv('results.csv')
attributes = result['antecedents']

numbers_list = extract_numbers_from_attributes(attributes)

list_to_csv(numbers_list, "numbers_list.csv")

remove_empty_rows('numbers_list.csv')

